<template>
  <div class="auth-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Sign up</h1>
          <p class="text-xs-center">
            <a href>Have an account?</a>
          </p>

          <ul class="error-messages">
            <li>That email is already taken</li>
          </ul>

          <form @submit.prevent="onSubmit">
            <fieldset class="form-group" v-if="!isLogin">
              <input
                v-model="user.username"
                class="form-control form-control-lg"
                type="text"
                placeholder="Your Name"
              />
            </fieldset>
            <fieldset class="form-group">
              <input
                v-model="user.email"
                class="form-control form-control-lg"
                type="text"
                placeholder="Email"
              />
            </fieldset>
            <fieldset class="form-group">
              <input
                v-model="user.password"
                class="form-control form-control-lg"
                type="password"
                placeholder="Password"
              />
            </fieldset>
            <button class="btn btn-lg btn-primary pull-xs-right">Sign up</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { register, login } from '@/api/auth'
import Cookie from 'js-cookie'
export default Vue.extend({
  name: 'LoginIndex',
  data() {
    return {
      user: {
        username: '',
        email: '',
        password: ''
      }
    }
  },
  methods: {
    async onSubmit() {
      try {
        // 获取表单数据 => 表单验证 => 提交表单
        const data = this.isLogin
          ? await login({ user: this.user })
          : await register({ user: this.user })
        // 本地化数据
        Cookie.set('t-user', data.user)
        // Vuex 数据
        this.$store.commit('login/setUser', data.user)
        this.$router.push('/')
        // data.user.email
        // 根据结果返回处理
        this.$router.push({ name: 'home' })
      } catch (err) {
        console.info('Submit Error...')
      }
    }
  },
  computed: {
    isLogin(): boolean {
      return this.$route.name === 'login'
    }
  }
})
</script>
